<?php
/**
 * ome模板处理
 * 此模块用于ome的模板service处理
 * @author chris.zhang
 * @name ome.delivery.template
 * @package ome_delivery_template
 * @copyright www.shopex.net.cn 2010.11.24
 *
 */
class ome_delivery_template{

    protected $elements = array(
        'ship_name'   => '收货人-姓名',
        'ship_area_0' => '收货人-地区1级',
        'ship_area_1' => '收货人-地区2级',
        'ship_area_2' => '收货人-地区3级',
        'ship_addr'   => '收货人-地址',
        'delivery_bn' => '收货人-发货单号',
        'ship_tel'    => '收货人-电话',
        'ship_mobile' => '收货人-手机',
        'ship_zip'    => '收货人-邮编',

        'dly_name'    => '发货人-姓名',
        'dly_area_0'  => '发货人-地区1级',
        'dly_area_1'  => '发货人-地区2级',
        'dly_area_2'  => '发货人-地区3级',
        'dly_address' => '发货人-地址',
        'dly_tel'     => '发货人-电话',
        'dly_mobile'  => '发货人-手机',
        'dly_zip'     => '发货人-邮编',

        'date_y'      => '当日日期-年',
        'date_m'      => '当日日期-月',
        'date_d'      => '当日日期-日',
    	'date_h'      => '当日日期-时',

        'order_bn'    => '订单-订单号',
        'order_count' => '发货单-物品数量',
        'order_memo'  => '订单-备注',
        'shop_name'   => '网店名称',
    		
    		

        //12.15需求，TODO 暂时注释
        'member_uname' => '会员名',
        'bn_amount' => '货号+数量',
        'name_amount' => '货品名+数量',
        'bn_name_amount' => '货号+货品名+数量',

        'tick'        => '对号 - √',
        'text'        => '自定义内容',
		'print_no' => '打印批次号',
		'is_protect'=>'是否保价√',
        'goods_fee'=>'声明价值',
        'cost_protect'=>'费用',
    		//2012-10-29 刘向峰
    		'delivery_amount' =>'快递单-总金额',
    		'delivery_amount_big' =>'快递单-总金额(大写)',
    );

    /**
     * default elements
     * 默认配置列表
     * @return array();
     */
    public function defaultElements(){
        return $this->elements;
    }

    /**
     * process default print content
     * 处理快递单打印项的对应内容
     * @param array $value_list
     * @return string
     */
    public function processElementContent($value_list){
        //12.15需求 ，TODO 暂时注释
		$prodctsObj = app::get('ome')->model('products');
		$goods_fee = 0;
        if ($value_list['delivery_items']) {
            foreach ($value_list['delivery_items'] as $item){
                //货号+数量
                $data['bn_amount'].= "货号：".$item['bn']."&nbsp;&nbsp;数量：".$item['number']."\n";
                //货品名+数量
                $data['name_amount'].= "货品名：".$item['product_name']."&nbsp;&nbsp;数量：".$item['number']."\n";
                //货号+货品名+数量
                $data['bn_name_amount'].= "货号：".$item['bn']."&nbsp;&nbsp;货品名：".$item['product_name']."&nbsp;&nbsp;数量：".$item['number']."\n";
				//声明价值（发货单中商品的销售价）
				 $tmp_price = $prodctsObj->getList('price',array('product_id'=>$item['product_id']));
				 $goods_fee += $tmp_price[0]['price'];
            }
        }
        //会员名
        if ($value_list['member_id']){
            $member = app::get('ome')->model('members')->dump($value_list['member_id']);
            $data['member_uname'] = $member['account']['uname'];
        }
        $data['ship_name']   = $value_list['consignee']['name'];
        $data['ship_addr']   = $value_list['consignee']['addr'];
        $data['ship_tel']    = $value_list['consignee']['telephone'];
        $data['ship_mobile'] = $value_list['consignee']['mobile'];
        $data['ship_zip']    = $value_list['consignee']['zip'];
        $data['ship_area_0'] = $value_list['consignee']['province'];
        $data['ship_area_1'] = $value_list['consignee']['city'];
        $data['ship_area_2'] = $value_list['consignee']['district'];
        $data['order_bn']    = $value_list['order_bn'];
        $data['order_count'] = $value_list['order_count'];
        $data['order_memo']  = $value_list['order_memo'];
        $data['delivery_bn'] = $value_list['delivery_bn'];

        $t = time()+($GLOBALS['user_timezone']-SERVER_TIMEZONE)*3600;
        $data['date_y'] = date('Y',$t);
        $data['date_m'] = date('m',$t);
        $data['date_d'] = date('d',$t);
        $data['date_h'] = date('H',$t);

        // 发货人信息
        if ($value_list['shopinfo']){
            $area = kernel::single('base_view_helper')->modifier_region($value_list['shopinfo']['area']);
            $area = explode('-',$area);
            $data['dly_area_0']     = $area[0];
            $data['dly_area_1']     = $area[1];
            $data['dly_area_2']     = $area[2];
            $data['dly_address']    = $value_list['shopinfo']['addr'];
            $data['dly_tel']        = $value_list['shopinfo']['tel'];
            $data['dly_mobile']     = $value_list['shopinfo']['mobile'];
            $data['dly_zip']        = $value_list['shopinfo']['zip'];
            $data['dly_name']       = $value_list['shopinfo']['default_sender'];
            $data['shop_name']      = $value_list['shopinfo']['name'];
        }

		$data['print_no'] = app::get('ome')->model('print_queue')->findFullIdent($value_list['delivery_id']);
		if($value_list['is_protect'] == 'true'){
			$is_protect = '√';
		}else{
			$is_protect = '';
		}
		$data['is_protect'] = $is_protect;
		//开启保价后的打印显示
		if($value_list['is_protect'] == 'true'){
		    $data['goods_fee'] = $goods_fee;
            $data['cost_protect'] = $value_list['cost_protect'];
		}

		$delivery_amount = $value_list['delivery_amount'];
		if($delivery_amount==0)$delivery_amount="";
		
		//刘向峰 2012-10-29		
		$data['delivery_amount']=$delivery_amount;//快递单总金额
		$data['delivery_amount_big']=$this->e2c($delivery_amount);//快递单总金额
		
        return $data;
    }
    
    function e2c($enum)
    {
    	if(!$enum){
    		$enum=0;
    	}
    	//获得一个整型数字变量，将其转换成中文的
    	//获得的数字串，下文中我称小数点“左”为“前”，“右”为“后”
    	$ary_chinese=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");//0-9
    	$ary_weishu=array("分","角","元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟");//仅仅支持到千亿。0－13
    	$err=true;//ture表示获得的$enum格式符合要求
    	if(!is_numeric($enum)) {
    		$err=false;$err_msg="请输入格式化金额（数字）";
    	}
    	if($enum=='0'){
    		$err=false;$err_msg="没有钱？不是吧？别开玩笑啦";
    	}//格式要求达到了，可是“0”在这里没什么用吧？
    	$enum=number_format($enum,2,'.','');//硬是把数字格式化成带两位小数点
    	if($err)
    	{
    		$len=strlen($enum);
    		if(3<$len&&$len<15)//正常范围内（千亿内）
    		{
    			$cnum="";//中文输出字符串初始化定义
    			if($len==4)
    			{
    				$curr_num=substr($enum,0,1);
    				if($curr_num=='0'){
    					$cnum="";
    				}else{$cnum=$ary_chinese[$curr_num]."元";
    				}
    			}
    			else
    			{
    				for($i=0;$i<$len-3;$i++)
    				{
    				if(is_int(($len-3-$i-1)/4))
    				{
    				$curr_bj='1';//当前处于边界
    				}
    				else
    				{
    				$curr_bj='0';//当前不处于边界
    				}
    				$curr_num=substr($enum,$i,1);//获得当前位数数值
    
    				if($fore_num<>'0')//当前数位的前(高)一位不为零
    				{
    				if($curr_num<>0)
    				{
    				$cnum.=$ary_chinese[$curr_num].$ary_weishu[$len-2-$i];//写数值也写单位
    				}
    				else //当前数值是零的话
    				{
    				if($curr_bj)
    				{
    				$cnum.=$ary_weishu[$len-2-$i];
    				//边界就是：亿，万，个位这些位置，这个位置的就算是零也要报一下单位
    				}
    				else
    				{
    				$cnum.=$ary_chinese[$curr_num];
    				//这里就是前面不为零，当前为零，肯定要写“零”
    				}
    				}
    					 
    				}
    				if($fore_num=='0')//当前数位的前(高)一位为零
    				{
    					if($curr_num<>0)
    					{
    					$cnum.=$ary_chinese[$curr_num].$ary_weishu[$len-2-$i];
    					//前面为零，当前不为零，写位数和单位。
    					}
    					else //当前数值是零的话
    					{
    					//前面是零，当前有是零的时候，要判断前面数字是否在边界
    					if($fore_bj)
    					{
    						$cnum.=$ary_chinese[$curr_num];
    					}//前字符在边界，当前数值是零，报一下“零”
    						else
    						{$cnum=$cnum;
    					}//前字符不在边界，前面是零的话，肯定已经填过“零”了，这里就不填了
    					}
    
    					}
    					$fore_num=$curr_num;
    					$fore_bj=$curr_bj;
    					}
    					}
    					 
    					//上面处理小数点左边
    					//下面处理小数点右边，由于位数少，用穷举反而方便
    					$ten=substr($enum,$len-2,1);//$ten为0.1
    					$hun=substr($enum,$len-2+1,1);//$hun为0.01
    					if($ten=='0'&&$hun=='0'){
    					$cnum.="整";
    				}
    				if($ten=='0'&&$hun<>'0'){
    				$cnum.=$ary_chinese[$ten].$ary_chinese[$hun].$ary_weishu[0];
    				}
    				if($ten<>'0'&&$hun<>'0'){
    				$cnum.=$ary_chinese[$ten].$ary_weishu[1].$ary_chinese[$hun].$ary_weishu[0];
    				}
    				if($ten<>'0'&&$hun=='0'){
    				$cnum.=$ary_chinese[$ten].$ary_weishu[1]."整";
    				}
    
    				//return "\$len=".$len."<br>\$enum=".$enum."<br>\$cnum=".$cnum;
    				//你想自己直观测试的话，把上行前面的注释去掉，同时给下面一行注释掉。
    				return $cnum;//输出结果中文输出字符串
    				}
    				else
    				{
    				$err=false;
    				$err_msg="超出范围，过千亿了。";
    				return "出错啦！<br>".$err_msg;
    				}
    
    				}
    				else
    				{
    				return "";
    				};
    				}
}